package com.ma.api;

import com.ma.service.OrderService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/orders")
public class OrderController {

    private final OrderService orderService;


    @PostMapping("/{productId}")
    public Map<String, Object> create(@PathVariable Integer productId) {
        return orderService.createOrder(productId);
    }


    @GetMapping("/headers")
    public Map<String, String> callProducer(HttpServletRequest request) {
        Map<String, String> headers = new HashMap<>();
        request.getHeaderNames().asIterator().forEachRemaining(header -> {
            headers.put(header, request.getHeader(header));
            log.info("Header: {} = {}", header, request.getHeader(header));
        });
        return orderService.getHeaders(headers);
    }

    @GetMapping("/hello")
    public String callProvider() {
        return orderService.hello();
    }
}
